﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.ViewModel;

namespace WMSSoft.WinKStart.ViewModel
{
    public class CWinKStartBehaviorViewModel : CViewModelBase
    {
        List<CDataItem> _SelectEventItems = null;
        List<CDataItem> _StyleItems = null;
        List<CDataItem> _RegionItems = null;
        List<CDataItem> _OpenItems = null;
        Boolean _EnableMoveButttonMouseMoveDelaytime = false;
        WMSSoft.Lib.WinKStart.Model.BehaviorSettings _SettingsObject = null;

        public CWinKStartBehaviorViewModel()
        {
            try
            {
                ReadSelectEventItems();
                ReadStyleItems();
                ReadRegionItems();
                ReadOpenItems();
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_INITIALIZE_SETTINGS_BEHAVIOR_VIEWMODEL, Ex, true, EMessageTypes.Error);
            }
        }

        public CWinKStartBehaviorViewModel(WMSSoft.Lib.WinKStart.Model.BehaviorSettings Settings)
        {
            try
            {
                //base.ViewName = ViewCaption;
                _SettingsObject = Settings;
                ReadSelectEventItems();
                ReadStyleItems();
                ReadRegionItems();
                ReadOpenItems();

                if (_SettingsObject.ActiveRegionOnStart.Length == 0)
                {
                    if (_RegionItems.Count > 0)
                    {
                        _SettingsObject.ActiveRegionOnStart = _RegionItems[0].Value1;
                    }
                }

                _SettingsObject.PropertyChanged += _SettingsObject_PropertyChanged;
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_INITIALIZE_SETTINGS_BEHAVIOR_VIEWMODEL, Ex, true, EMessageTypes.Error);
            }
        }

        void _SettingsObject_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                if (e.PropertyName == "MoveButtonMouseOpen")
                {
                    if (Data.MoveButtonMouseOpen > 0)
                        EnableMoveButtonMouseOpenDelayTime = true;
                    else
                        EnableMoveButtonMouseOpenDelayTime = false;
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_CHANGE_SETTINGS_MODEL_PROPERTY, Ex, true, EMessageTypes.Error);
            }
        }

        public bool EnableMoveButtonMouseOpenDelayTime
        {
            get { return _EnableMoveButttonMouseMoveDelaytime; }
            set
            {
                _EnableMoveButttonMouseMoveDelaytime = value;
                OnPropertyChanged("EnableMoveButtonMouseOpenDelayTime");
            }
        }

        public List<CDataItem> SelectEventItems {
            get { return _SelectEventItems; }
            private set { 
                _SelectEventItems = value;
                OnPropertyChanged("SelectEventItems");
            }
        }

        public List<CDataItem> StyleItems
        {
            get { return _StyleItems; }
            set
            {
                _StyleItems = value;
                OnPropertyChanged("StyleItems");
            }
        }

        public List<CDataItem> RegionItems
        {
            get { return _RegionItems; }
            set
            {
                _RegionItems = value;
                OnPropertyChanged("RegionItems");
            }
        }

        public List<CDataItem> OpenItems
        {
            get { return _OpenItems; }
            set
            {
                _OpenItems = value;
                OnPropertyChanged("OpenItems");
            }
        }

        public WMSSoft.Lib.WinKStart.Model.BehaviorSettings Data
        {
            get { return _SettingsObject; }            
        }

        void ReadSelectEventItems()
        {
            try
            {
                SelectEventItems = new List<CDataItem>();
                SelectEventItems.Clear();
                SelectEventItems.Add(new CDataItem("MouseClick", LanguageStrings.GetText((string)"Mouse Click")));
                SelectEventItems.Add(new CDataItem("MouseMove", LanguageStrings.GetText((string)"Mouse Move")));
                OnPropertyChanged("SelectEventItems");
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_LOAD_REGION_SELECTEVENTS, Ex, true, EMessageTypes.Error);
            }
        }

        void ReadStyleItems()
        {
            try
            {
                StyleItems = new List<CDataItem>();
                StyleItems.Clear();
                Dictionary<string, WMSSoft.Lib.WinKStart.Classes.Styles.cUIStyle>.KeyCollection StyleNames = cUserSettingsReader.StyleReader.StyleViewNames.Keys;
                foreach (string Style in StyleNames)
                {
                    StyleItems.Add(new CDataItem(Style, Style));
                }
                OnPropertyChanged("StyleItems");
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_LOAD_STYLES, Ex, true, EMessageTypes.Error);
            }
        }

        void ReadRegionItems()
        {
            try
            {
                RegionItems = new List<CDataItem>();
                RegionItems.Clear();              

                Dictionary<string, cRegion>.KeyCollection tmpKeys = cMenuConfigReader.TabPages.Keys;

                foreach (string KeyItem in tmpKeys)
                {
                    RegionItems.Add(new CDataItem(KeyItem, LanguageStrings.GetText((string)cMenuConfigReader.TabPages[KeyItem].Caption)));                    
                }


            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_LOAD_REGIONS, Ex, true, EMessageTypes.Error);
            }
        }

        void ReadOpenItems()
        {
            try
            {
                OpenItems = new List<CDataItem>();

                OpenItems.Add(new CDataItem("0", LanguageStrings.GetText("Nothing")));
                OpenItems.Add(new CDataItem("1", LanguageStrings.GetText("WinKStart Startmenu")));
                OpenItems.Add(new CDataItem("2", LanguageStrings.GetText("Metro - StartScreen")));
                OnPropertyChanged("OpenItems");
            }
            catch (Exception Ex) 
            {
                CMessagelogging.AppendMessage(ErrorMessages.USERSETTINGS__ERROR_UNABLE_TO_LOAD_STARTBUTTON_MODES, Ex, true, EMessageTypes.Error);
            }
        }        
    }
}
